package com.tiantian.app.data;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.tiantian.app.data.mo.XmlImage;

public class GallerySizeRssHandler extends DefaultHandler{
	private List<XmlImage> xmlImage;
	private XmlImage currentXmlImage;
	private StringBuilder builder;
	@SuppressWarnings("unused")
	private boolean bolGetImage;
	
	public List<XmlImage> getXmlImage(){
		return xmlImage;
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
//		Log.e("endElement", bolGetImage+uri+"=====  localName:"+localName+" name:"+name+"   "+builder.toString());
		if (this.currentXmlImage != null){
			if (localName.equalsIgnoreCase("sizes")){
				xmlImage.add(currentXmlImage);
//				Log.e("XmlImage", currentXmlImage.getDescription()+"+++"+currentXmlImage.getImgUrl()+
//						"+++"+currentXmlImage.getLink()+"+++"+currentXmlImage.getTitle()+"###");
			}
			
			builder.setLength(0);	
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		xmlImage = new ArrayList<XmlImage>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
//		Log.e("startElement", "***   uri:"+uri+"  localName:"+localName+" name:"+name+"  attributes:"+attributes);
		if (localName.equalsIgnoreCase("sizes")){
			this.currentXmlImage = new XmlImage();
		}

        if (localName.equalsIgnoreCase("size")){      //get .jpg url
        	String source = attributes.getValue("source");    
        	Log.e("startElement source","=========== "+source);
        	if("Small".equals(attributes.getValue("label"))){
	            currentXmlImage.setSource(source);   
        	}
        	if(currentXmlImage.getSource()==null){
        		currentXmlImage.setSource(source);
        	}
            	
		} 
					
	}
}